﻿using AgileDevTool.Helpers;
using AgileDevTool.UserControls;
using System;
using System.IO;
using System.Linq;
using System.Windows;
using System.Windows.Input;

namespace AgileDevTool.Windows
{
    /// <summary>
    /// ChooseProjectWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ChooseProjectWindow : Window
    {
        public delegate void ProjectChoosedEventHandler(string path);

        public event ProjectChoosedEventHandler ProjectChoosed;

        public ChooseProjectWindow()
        {
            InitializeComponent();
            Loaded+=ChooseProjectWindow_Loaded;
        }

        private void ChooseProjectWindow_Loaded(object sender, RoutedEventArgs e)
        {
            if (CacheHelper.Projects!=null && CacheHelper.Projects.Count>0)
            {
                var projects = CacheHelper.Projects.Where(w => w.LastOpenedAt.HasValue).OrderByDescending(o => o.LastOpenedAt.GetValueOrDefault()).ToList();
                for (var i = projects.Count()-1; i>=0; i--)
                {
                    var item = projects[i];
                    if (!Directory.Exists(item.Path))
                    {
                        projects.RemoveAt(i);
                        continue;
                    }

                    var uc = new ProjectItemUserControl(item);
                    uc.Margin = new Thickness(5);
                    uc.ProjectItemClicked+=Uc_ProjectItemClicked;
                    sp_Projects.Children.Add(uc);
                }
            }
        }

        private void Uc_ProjectItemClicked(string path)
        {
            Close();
            ProjectChoosed(path);
        }

        private void CreateNewProject(object sender, MouseButtonEventArgs e)
        {
            var name = tbx_Name.Text;
            if (string.IsNullOrEmpty(name))
            {
                UIHelper.Toast(sp_Toast, "请填写项目名称");
                return;
            }

            var path = tbx_Path.Text;
            if (string.IsNullOrEmpty(path))
            {
                UIHelper.Toast(sp_Toast, "请填写文件目录");
                return;
            }

            if (path.IndexOf(":\\")<1)
            {
                UIHelper.Toast(sp_Toast, "文件目录无效");
                return;
            }

            var one = CacheHelper.Projects.Where(w => w.Path.ToLower()==path.ToLower());
            if (!one.Any())
            {
                var project = new ProjectItem
                {
                    LastOpenedAt=DateTime.Now,
                    Name = name,
                    Path = path
                };
                CacheHelper.Projects.Add(project);
                if (!Directory.Exists(path))
                {
                    Directory.CreateDirectory(path);
                    RealCreateNewProject(project);
                }
            }

            Close();
            ProjectChoosed(path);
        }

        private void RealCreateNewProject(ProjectItem project)
        {
            var folder = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "Projects", "Demo");
            if (!Directory.Exists(folder))
            {
                LogHelper.WriteErrorAsync($"未找到目录：{folder}");
                return;
            }

            FileHelper.CopyFolder(folder, project.Path);
        }
    }
}
